let addDish = true
let isSteal = false
let minGap = 350
let maxGap = 600
let stealSeconds = 65

let time = 500
let stealDegree = 0
let degree = 0
let lastUncommonDish = null

const storageKey = 'bili-2020-year'
const menu = []
const uncommonMenu = []
const addButton = document.querySelector('.button.add.ibg.normal').children[4]
const stealButton = document.querySelector('.button.minus.ibg.normal').children[4]
const dishLayer = document.querySelector('.top-tip').children[0]
const eventManager = new EventTarget()

const getRandomIntNum = (min, max) => Math.floor(Math.random() * (max - min)) + min

const loop = () => {
    setTimeout(() => {
        if (addDish) {
            time = getRandomIntNum(minGap, maxGap)
            degree++
            addButton.click()
            collectDish()
            // console.log('下次点击：', time, '毫秒后')
            eventManager.dispatchEvent(new Event('refreshMsg'))
            eventManager.dispatchEvent(new Event('save'))
        }
        loop()
    }, time)
}

// 收集稀有食材
const collectUncommon = () => {
    try {
        const currentUncommonDish = document.querySelector('.special-food-get-daze').children[1].children[1].children[1].innerText
        if (lastUncommonDish !== currentUncommonDish) {
            if (uncommonMenu.indexOf(currentUncommonDish) === -1) {
                uncommonMenu.push(currentUncommonDish)
            }
            lastUncommonDish = currentUncommonDish
        }
    } catch (e) {
        // console.log('木有抓到稀有食材噢~')
    }

}

// 收集普通食材
const collectDish = () => {
    try {
        const dish = dishLayer.children[1].innerText.split(' ')[1]
        if (dish === '-') { // 偷吃的时候字符格式和加菜不同
            return
        }
        // console.log(dish)
        if (menu.indexOf(dish) === -1) {
            menu.push(dish)
        }
        collectUncommon()
    } catch (e) {
        console.log('再加点菜再开始')
    }

}

const showMenu = () => {
    let str = '总共加菜 ' + degree + ' 次'
    str += '\n统计到普通食材 ' + menu.length + ' 种 \n'
    for (let i = 0; i < menu.length; i++) {
        const dish = menu[i]
        str += dish
    }
    str += '\n统计到稀有食材 ' + uncommonMenu.length + ' 种 \n'
    for (let i = 0; i < uncommonMenu.length; i++) {
        const dish = uncommonMenu[i]
        str += dish + ' '
    }
    console.log(str)
}

// 停止加菜
const stopAdd = () => {
    addDish = false
}

// 开始加菜
const beginAdd = () => {
    addDish = true
}

// 生成控制面板
const createLayer = () => {
    const layer = document.createElement('div')
    layer.style = 'display: block; padding: 0px; margin: 0px; z-index: 999; position: fixed; top:0; left: 0; background-color:rgba(255,255,255,0.8);'
    document.body.appendChild(layer)
    return layer
}

const createBeginButton = (layer) => {
    const btn = document.createElement('button')
    btn.innerText = '开始加菜'
    btn.style = 'margin: 3px;font-size: 10px;'
    layer.appendChild(btn)
    btn.onclick = beginAdd
}

const createStopButton = (layer) => {
    const btn = document.createElement('button')
    btn.innerText = '停止加菜'
    btn.style = 'margin: 3px;font-size: 10px;'
    layer.appendChild(btn)
    btn.onclick = stopAdd
}

const createMessageLog = (layer) => {
    const prg = document.createElement('p')
    prg.innerText = '总共加菜 ' + degree + ' 次\n'
    prg.style = 'margin: 3px;font-size: 10px;line-height:15px'
    prg.id = 'message-log-prg'
    layer.appendChild(prg)
}

const createPanel = () => {
    let layer = createLayer()
    createBeginButton(layer)
    createStopButton(layer)
    createMessageLog(layer)
}

const refreshMessageLog = () => {
    try {
        const prg = document.getElementById('message-log-prg')
        let str = '下次加菜在 ' + time + ' 毫秒后'
        str += '\n总共加菜 ' + degree + ' 次'
        str += '\n总共偷吃 ' + stealDegree + ' 次'
        str += '\n统计到普通食材 ' + menu.length + ' 种'
        str += '\n统计到稀有食材 ' + uncommonMenu.length + ' 种'
        prg.innerText = str
    } catch (e) {

    }

}

// 事件初始化
const eventBiliInit = () => {
    eventManager.addEventListener('refreshMsg', refreshMessageLog)
    eventManager.addEventListener('save', saveData)
}

// 数据初始化
const dataInit = () => {
    let data = localStorage.getItem(storageKey)
    if (data !== null) {
        console.log('成功读取存档')
        data = JSON.parse(data)
        degree = data.degree
        stealDegree = data.stealDegree
        data.menu.map(i => menu.push(i))
        data.uncommonMenu.map(i => uncommonMenu.push(i))

    } else {
        console.log('木有存档')
    }
}

// 保存数据
const saveData = () => {
    let data = {}
    data.degree = degree
    data.stealDegree = stealDegree
    data.menu = menu
    data.uncommonMenu = uncommonMenu

    data = JSON.stringify(data)
    localStorage.setItem(storageKey, data)
}

setInterval(() => {
    showMenu()
}, 30 * 1000)

setInterval(() => {
    if (isSteal) {
        stealDegree++
        stealButton.click()
        console.log('偷吃成功, 偷吃次数：', stealDegree)
    }
}, stealSeconds * 1000)

dataInit()
createPanel()
eventBiliInit()
loop()